/************************************************************************//**
 * @file cliente.cpp
 *
 * Cuenta con las funciones:
 *
 * - Conectar
 * - Enviar (a traves de un objeto TCP)
 * - Recibir (a traves de un objeto TCP)
 *
 *
 *//************************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <cstdlib>
#include <string.h>
#include <fstream>
#include "cliente.h"
#include "TCP.h"
#include "sockets_.h"


Cliente::Cliente() {}

TCP Cliente::conectar(int puerto, char ip[])
{
    portno = puerto;
    socketfd = socket(AF_INET, SOCK_STREAM, 0);
    if (socketfd < 0)
    {
        printf("ERROR opening socket");
        exit(0);
    }
    tcp.setFileDescriptor(socketfd);
    server = gethostbyname(ip);
    if (server == NULL)
    {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr,
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(socketfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
    {
        printf("error");
        exit(0);
    }
    return tcp;
}

